//
//  ZhuanTiViewController.m
//  TravelYo
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 TravelYo. All rights reserved.
//

#import "ZhuanTiViewController.h"
#import "ZhuanTiViewCell.h"
#import "DetailZhuanTiViewController.h"

@interface ZhuanTiViewController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ZhuanTiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // self.navigationController.navigationBar.backgroundColor = kMainColor;
    // self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    
    self.navigationItem.title = @"专题";
    
    // 设置cell分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:@"ZhuanTiViewCell" bundle:nil] forCellReuseIdentifier:@"zhuanTiCell"];
    // 加载数据
    [self setUpData];
    // 刷新
    [self setUpRefresh];
    
}

- (void)setUpData
{
    
    [[TYDataManager shareManager] requestDataWithUrl:kZhuanTiAPI  block:^(NSData *data){
        
        // 刷新
        if (!self.isRefresh) {
            [self.dataArray removeAllObjects];
        }
        
       // NSLog(@"%@", data);
        // 解析数据
        NSMutableDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray *array = dataDic[@"data"];
        for (NSDictionary *dic in array) {
            ZhuanTiModel *model = [[ZhuanTiModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
          //  NSLog(@"=====%@", model);
            [self.dataArray addObject:model];
           // NSLog(@"%@", self.dataArray);
            }
        
        [self.tableView reloadData];
    }];

}

// 懒加载方法
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark --- 刷新

- (void)setUpRefresh
{
    // 下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    [self.tableView.header beginRefreshing];
    
  
}

// 实现下拉刷新的方法
- (void)headerRereshing
{
   
    self.isRefresh = NO;
    [self setUpData];
    [self.tableView.header endRefreshing];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZhuanTiViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zhuanTiCell" forIndexPath:indexPath];
    // 数据保护
    if (self.dataArray.count > 0) {
        ZhuanTiModel *model = self.dataArray[indexPath.row];
        [cell.photoImage sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:nil];
        
    }
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190;
}


// 点击进入详情页面的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailZhuanTiViewController *detailZTVC = [[DetailZhuanTiViewController alloc] init];
    
    ZhuanTiModel *model = self.dataArray[indexPath.row];
    detailZTVC.url = model.url;
    // 当跳转的时候隐藏tabBar
    detailZTVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailZTVC animated:YES];
    
}





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
